<template>
    <div class="login-container">
        <!-- 顶部Logo和标题 -->
        <div class="header">
            <div class="logo-container">
                <van-image width="70" height="70" src="https://fastly.jsdelivr.net/npm/@vant/assets/logo.png" round />
            </div>
            <h1 class="app-title">AI律师咨询助手</h1>
        </div>

        <!-- 登录表单 -->
        <div class="login-form">
            <van-form @submit="onSubmit">
                <van-cell-group inset>
                    <van-field v-model="username" name="username" placeholder="请输入用户名" :rules="[
                        { required: true, message: '请输入用户名' },
                        // { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号' }
                    ]">
                        <template #left-icon>
                            <van-icon name="user-circle-o" class="field-icon" />
                        </template>
                    </van-field>

                    <van-field v-model="password" type="password" name="password" placeholder="请输入密码" :rules="[
                        { required: true, message: '请填写密码' },
                        { min: 6, message: '密码长度不能少于6位' }
                    ]">
                        <template #left-icon>
                            <van-icon name="lock" class="field-icon" />
                        </template>
                    </van-field>
                </van-cell-group>

                <div class="submit-btn">
                    <van-button round block type="primary" native-type="submit">
                        登录
                    </van-button>
                </div>

                <div class="register-tip">
                    <span @click="goToRegister">还没有账号？<span class="register-link">立即注册</span></span>
                </div>

                <div class="divider">
                    <span class="divider-line"></span>
                    <span class="divider-text">或</span>
                    <span class="divider-line"></span>
                </div>

                <div class="wechat-login" @click="weChatLogin">
                    <van-icon name="wechat" size="22" color="#07C160" />
                    <span class="wechat-text">微信一键登录</span>
                </div>
            </van-form>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { showToast } from 'vant';
import { useRouter } from 'vue-router';
import { showNotify } from 'vant';

import { ElMessage,ElLoading } from 'element-plus';
import { loginApi } from '@/api/login_register';

const router = useRouter();
const username = ref('');
const password = ref('');

const onSubmit = async (values: any) => {
    console.log('submit', values);

    try {
        const loading = ElLoading.service({
             lock: true,
             text: '登录中',
             background: 'rgba(0, 0, 0, 0.7)',
           });
           
        const response = await loginApi({
            username: values.username,
            password: values.password
        });

        if (response.data.code === 200) {
            // 1. 存储用户信息
            console.log(response.data.data.isAdmin);
            console.log(response.data.data.token);

            localStorage.setItem('role-isAdmin', response.data.data.isAdmin == 1 ? 'admin' : 'user');
            localStorage.setItem('token', response.data.data.token);
            localStorage.setItem('userNo', response.data.data.userNo);
            localStorage.setItem('username', response.data.data.username);
            localStorage.setItem('avatar', response.data.data.avatar);
            loading.close();
            ElMessage.success('登录成功');
            // 3. 跳转到首页
            router.push('/');

        } else {
            loading.close();
            ElMessage.error(response.data.msg || '登录失败');
        }
    } catch (error) {
        console.error('登录出错:', error);
        ElMessage.error('登录失败，请检查用户名和密码');
    }
};


const goToRegister = () => {
    // showToast('跳转到注册页面');
    router.push('/register');
};

const weChatLogin = () => {
    // showToast('跳转到微信登录');
    showNotify({ type: 'primary', message: '跳转到微信登录' });
};

</script>

<style>
/* 重置全局样式 */
html,
body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
</style>

<style scoped>
/* 整体容器样式 - 撑满全屏 */
.login-container {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    flex-direction: column;
    background-color: #f5f7fa;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

/* 返回按钮样式 */
.back-button {
    position: fixed;
    top: 12px;
    left: 12px;
    font-size: 20px;
    color: #333;
    z-index: 100;
    padding: 8px;
    background-color: rgba(255, 255, 255, 0.8);
    border-radius: 50%;
    backdrop-filter: blur(5px);
}

/* 顶部标题区域 - 缩小间距 */
.header {
    width: 100%;
    padding-top: 40px;
    /* 从60px减小到40px */
    text-align: center;
}

.logo-container {
    margin-bottom: 10px;
    /* 从15px减小到10px */
}

.app-title {
    font-size: 18px;
    font-weight: 600;
    color: #333;
    margin: 0 0 15px;
    /* 从20px减小到15px */
    letter-spacing: 1px;
}

/* 登录表单样式 - 上移 */
.login-form {
    width: 100%;
    padding: 0 16px;
    box-sizing: border-box;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    /* 从center改为flex-start */
    margin-top: 10px;
    /* 添加负外边距让表单上移 */
}

/* 表单卡片样式 */
:deep(.van-cell-group--inset) {
    margin: 0;
    border-radius: 8px;
    overflow: hidden;
}

/* 输入框样式 */
:deep(.van-field) {
    padding: 14px 16px;
}

.field-icon {
    margin-right: 12px;
    color: #1989fa;
    font-size: 18px;
}

/* 提交按钮 */
.submit-btn {
    margin: 20px 0 15px;
    /* 从25px减小到20px */
}

/* 注册提示 */
.register-tip {
    text-align: center;
    margin: 15px 0;
    font-size: 14px;
    color: #666;
}

.register-link {
    color: #1989fa;
    font-weight: 500;
}

/* 分割线 */
.divider {
    display: flex;
    align-items: center;
    margin: 18px 0;
    /* 从20px减小到18px */
}

.divider-line {
    flex: 1;
    height: 1px;
    background-color: #eee;
}

.divider-text {
    padding: 0 12px;
    color: #999;
    font-size: 12px;
}

/* 微信登录 */
.wechat-login {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: 15px;
    padding: 10px;
}

.wechat-text {
    margin-top: 8px;
    color: #07C160;
    font-size: 14px;
}

/* 平板及以上设备适配 */
@media (min-width: 768px) {
    .login-form {
        max-width: 400px;
        margin: 10px auto 0;
        /* 调整上边距 */
        padding: 20px;
    }

    :deep(.van-cell-group--inset) {
        border-radius: 12px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    }

    .header {
        padding-top: 60px;
        /* 从80px减小到60px */
    }

    .app-title {
        font-size: 20px;
        margin-bottom: 20px;
        /* 从30px减小到20px */
    }
}
</style>